package com.objetdirect.gwt.umldrawer.client;

import java.util.ArrayList;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.FileUpload;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.util.Format;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.MessageBoxConfig;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.ToolbarMenuButton;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.menu.BaseItem;
import com.gwtext.client.widgets.menu.Item;
import com.gwtext.client.widgets.menu.Menu;
import com.gwtext.client.widgets.menu.event.BaseItemListenerAdapter;

public class DrawerToolbar {


//	Panel panel = new Panel();
	Toolbar toolbar = new Toolbar();
	 
	private  Menu file = new Menu(); 
	private  Menu notifications = new Menu();  
	private  Menu diagrams = new Menu();
	
//	private  Item newProject = new Item("New Project");
//	private  Item openProject = new Item("Open Project"); 
	private  Item save = new Item("Save");  
	private  Item logOut = new Item("Log Out..");  
	private  Item importDiagram = new Item("Import Diagram");
	private  Item export = new Item("Export Diagram");
	private  Item newDiagram = new Item("New Diagram");

	private  ToolbarButton notes = new ToolbarButton("Notes");
	private  ToolbarButton projects = new ToolbarButton("Projects");
	private  ToolbarButton profile = new ToolbarButton("Profile");
	private  ToolbarButton workSpace = new ToolbarButton("Work Space");
	private  ToolbarButton inbox = new ToolbarButton("Inbox");
	private  ToolbarButton menuButton = new ToolbarButton("File");  
	private  ToolbarButton diagramMenu = new ToolbarButton("Diagrams");

	ToolbarMenuButton notificationToolBar = new ToolbarMenuButton(
			"Notifications", notifications);

	// array list hatgili mn DB fiha el notifications eli UnRead
	ArrayList<String> notificationsItems = new ArrayList<String>();

	public Toolbar getToolBar() {
//		panel.setBorder(false);

		// create a toolbar and various menu items
		toolbar.setWidth("100%");
		toolbar.setAutoHeight(true);

		// File Menu
		file.setShadow(true);
		// file.setMinWidth(10);

		// file menu items
//		newProject.setIcon("/images/new project.png");
//		openProject.setIcon("/images/open.png");
		save.setIcon("/images/save.png");
		logOut.setIcon("/images/logOut.png");

		// file menu items' Listeners
		save.addListener(new BaseItemListenerAdapter() {
			@Override
			public void onClick(BaseItem item, EventObject e) {
				// TODO Auto-generated method stub
				MessageBox.alert("Changes saved successfully");
			}
		});
		logOut.addListener(new BaseItemListenerAdapter() {
			@Override
			public void onClick(BaseItem item, EventObject e) {
				// TODO Auto-generated method stub
				MessageBox.show(new MessageBoxConfig() {
					{
						setTitle("Save Changes?");
						setMsg("Would you like to save your changes before log out ?");
						setButtons(MessageBox.YESNOCANCEL);
						setCallback(new MessageBox.PromptCallback() {
							public void execute(String btnID, String text) {
								System.out.println("Button Click : "
										+ Format.format(
												"You clicked the {0} button",
												btnID));
							}
						});
					}
				});
			}
		});

		// add items to file menu
//		file.addItem(newProject);
//		file.addItem(openProject);
		file.addItem(save);
		file.addItem(logOut);

		// add file menu to toolbar
		menuButton.setMenu(file);
		toolbar.addButton(menuButton);

		toolbar.addSeparator();

		// end of file menu

		// diagram menu
		diagrams.setShadow(true);
		diagrams.setMinWidth(10);

		// diagram menu items
		newDiagram.setIcon("/images/newDiagram.png");
		importDiagram.setIcon("/images/import.png");
		export.setIcon("/images/export.png");

		// diagram menu items' listeners
		newDiagram.addListener(new BaseItemListenerAdapter() {
			@Override
			public void onClick(BaseItem item, EventObject e) {
				// TODO Auto-generated method stub
				System.out.println("hello");
			}
		});
		importDiagram.addListener(new BaseItemListenerAdapter() {
			@Override
			public void onClick(BaseItem item, EventObject e) {
				// TODO Auto-generated method stub
				FileUpload importDiagram = new FileUpload();
				importDiagram.setName("uploadFormElement");

				Window importWindow = new Window();
				importWindow.setTitle("Import Diagram");
				importWindow.setModal(true);
				importWindow.add(importDiagram);
				importWindow.show();
			}
		});
		export.addListener(new BaseItemListenerAdapter() {
			@Override
			public void onClick(BaseItem item, EventObject e) {
				// TODO Auto-generated method stub

			}
		});

		// add items to diagram menu
		diagrams.addItem(newDiagram);
		diagrams.addItem(importDiagram);
		diagrams.addItem(export);

		// add diagram menu to toolbar
		diagramMenu.setMenu(diagrams);
		toolbar.addButton(diagramMenu);

		toolbar.addSeparator();
		// end of diagram menu

		// notifications menu
		notifications.setShadow(true);

		// de items bdl mn eli mn DB till ma ngbha mn el DB
		notificationsItems.add("Admin added new member");
		notificationsItems.add("Class Diagram is now unlocked");
		notificationsItems.add("dodo added new diagram");

		// add notifications' items to notification menu
		for (int i = 0; i < notificationsItems.size(); i++) {
			Item item = new Item(notificationsItems.get(i));
			item.setIcon("/images/notifications.png");
			notifications.addItem(item);
			notifications.addSeparator();
		}

		toolbar.addButton(notificationToolBar);
		toolbar.addSeparator();
		// end of notification menu

		workSpace.addListener(new ButtonListenerAdapter()
		{
			@Override
			public void onClick(Button button, EventObject e) {
				History.newItem("WorkSpace");
			}
		});
		toolbar.addButton(workSpace);
		toolbar.addSeparator();

		// Adds a fill element that forces subsequent additions to the right
		// side of the toolbar.
		toolbar.addFill();

		toolbar.addSeparator();

		// projects button
		projects.setIconCls("projectsToolBarButtonCss");
		toolbar.addButton(projects);
		
		// notes button
		notes.setIconCls("noteCss");
		toolbar.addButton(notes);
		

		toolbar.addSeparator();
		// end of notes button

		inbox.setIconCls("inboxButtonToolBar");
		toolbar.addButton(inbox);

		toolbar.addSeparator();

		profile.setIconCls("myProfile");
		toolbar.addButton(profile);

//		panel.add(toolbar);
		
		return toolbar;
	}
}
